package com.wu.server.service;

import com.wu.server.code.Code;
import com.wu.server.config.EncodeJson;
import com.wu.server.dao.GoodsRepository;
import com.wu.server.dao.GoodsTypeRepository;
import com.wu.server.entity.Goods;
import com.wu.server.entity.Type;
import com.wu.server.websocket.TypePercentageWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.websocket.EncodeException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 商品种类top7
 */
@Service
public class TypeTopServer {
    @Autowired
    GoodsRepository goodsRepository;
    @Autowired
    GoodsTypeRepository goodsTypeRepository;
    @Autowired
    Code code;
    @Autowired
    EncodeJson encodeJson;
    /**
     * // TODO: 2021/3/3 外部解析type标签数据
     */
    @Async
    public void execute(){
        saveGoodsTypeSum();//存取type_sum
        this.sendData();//发送消息
    }
    public void sendData(){
        HashMap<String ,Object> result = new HashMap<>();
        result.put("goods",getTypeNum());//存取data
        result.put("sales",getTypeSales());
        HashMap<String,Object> meta = new HashMap<>();
        meta.put("code", code.TYPENUM);
        meta.put("msg",code.getMsg(code.TYPENUM));
        result.put("meta",meta);
        try {
            TypePercentageWebSocket.sendMessage(encodeJson.encode(result));//发送消息
        } catch (EncodeException e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询所有价格排序后的标签
     * @param
     */
    private HashMap<String, Object> getTypeNum(){
        Iterable<Type> type_list = goodsTypeRepository.getPrinceOrder();
        HashMap<String,Object> info = new HashMap<>();
        List<Object> xAxis = new ArrayList<>();
        List<Object> type_num = new ArrayList<>();
        for(Type type : type_list){
            xAxis.add(type.getType_name());
            type_num.add(type.getType_sum());
        }
        info.put("xAxis" ,xAxis);
        info.put("data",type_num);
        return info;
    }

    /**
     * 返回销量数据
     * @return
     */
    private HashMap<String, Object> getTypeSales(){
        Iterable<Type> type_list = goodsTypeRepository.getSalesOrder();
        HashMap<String,Object> info = new HashMap<>();
        List<Object> xAxis = new ArrayList<>();
        List<Object> type_sales = new ArrayList<>();
        for(Type type : type_list){
            xAxis.add(type.getType_name());
            type_sales.add(type.getType_sales());
        }
        info.put("xAxis" ,xAxis);
        info.put("data",type_sales);
        return info;
    }

    /**
     * // TODO: 2021/3/3 存储标签储量到type_sum
     */
    private void saveGoodsTypeSum(){
        Iterable<Type> typeList = goodsTypeRepository.findAll();
        for(Type item : typeList){
            saveGoodsTypeSales(item);//存取sales
        }
    }
    // TODO: 2021/3/4 保存type_salaes数量
    private void saveGoodsTypeSales(Type item){
        Iterable<Goods> goods_list = getGoods(item.getType_name());
        int sales  = 0;
        Type type = new Type();
        for (Goods goods:goods_list){
            sales+=goods.getSales();
        }
        type.setId(item.getId());
        type.setType_name(item.getType_name());
        type.setType_sum(getNum(item.getType_name()));
        type.setType_sales(sales);
        goodsTypeRepository.save(type);
    }
    /**
     * 获取type数量
     * @param type_name = 商品名称like
     * @return Interger
     */
    private Integer getNum(String type_name){
        type_name = "%"+type_name+"%";
        return goodsRepository.SumType(type_name);
    }
    /**
     * 查询goods数据
     * @param type_name = 商品名称like
     * @return Interger
     */
    private Iterable<Goods> getGoods(String type_name){
        type_name = "%"+type_name+"%";
        return goodsRepository.goodsList(type_name);
    }


}
